import Vuex from 'vuex'
import ehHome from '@/net/ehHome'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    year: new Date().getFullYear(),
    userAdcd: '',
    adcd: '',
    adcdName: [],
    adcdId: [],
    energyType: '0',
    gridHead: [
      { name: 'XZQHMC', value: '行政区划'},
      { name: 'total', value: '合计'}
    ],
    gridData: [],
    energyTypeData: [
      { name: '能源类型', value: '0'},
      { name: '供暖方式', value: '1'}
    ],
    company: ''
  },
  mutations: {
    changeYear (state, value) {
      state.year = value
    },
    changeUserAdcd (state, value) {
      state.userAdcd = value
    },
    changeAdcd (state, value) {
      state.adcd = value
    },
    changeAdcdName (state, value) {
      state.adcdName = value
    },
    changeAdcdId (state, value) {
      state.adcdId = value
    },
    changeEnergyType (state, value) {
      state.energyType = value
    },
    changeGridHead (state, value) {
      state.gridHead = value
    },
    changeGridData (state, value) {
      state.gridData = value
    },
    changeCompany (state, value) {
      state.company = value
    }
  },
  actions: {
    getLedger ({commit, state}, vm) {
      vm.$notice.loading.show()
      ehHome.getEHHome({
        nd: state.year,
        quyxzqhdm: state.adcd,
        quycjdm: state.company
      }).then(res => {
        let arrHead = [
          { name: 'XZQHMC', value: '行政区划'},
          { name: 'total', value: '合计'}
        ]
        if (state.energyType === '0') {
          res.items.forEach((value, index) => {
            arrHead.push({
              name: 'ynfs' + index,
              value: value.YNFSDLMC + '(户/台)'
            }) 
          })
          res.datalist.forEach(data => {
            res.items.forEach((value, index) => {
              data['ynfs' + index] = data['NHSLYNFSDL' + value.YNFSDL] + '/' + data['SBSLYNFSDL' + value.YNFSDL]
            })
            data.total = data.NHSLYNFSDLSUM + '/' + data.SBSLYNFSDLSUM
          })
        } else {
          res.itemsList.forEach((value, index) => {
            arrHead.push({
              name: 'gnfs' + index,
              value: value.YNFSDLMC + '(户/台)'
            })
          })
          res.datalist.forEach(data => {
            res.itemsList.forEach((value, index) => {
              data['gnfs' + index] = data['NHSLGNFS' + value.YNFSDL] + '/' + data['SBSLGNFS' + value.YNFSDL]
            })
            data.total = data.NHSLYNFSDLSUM + '/' + data.SBSLYNFSDLSUM
          })
        }
        commit('changeGridHead', arrHead)
        commit('changeGridData', res.datalist)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    }
  }
})
